﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Resources;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;

namespace ServicioWebRest.Areas.Api.Models
{
    public class NotificacionManager
    {
        ResourceManager rm = new ResourceManager(typeof(RecursosWeb));

        public bool CrearNotificacion(string usu, string local, string tipo)
        {
            try
            {
                // Create the command and the connection.
                string sql = "[sp_extrae_notificacion] '" + usu + "'," + local + "," + tipo;
                SqlConnection con = new SqlConnection(rm.GetString("connectionString"));
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataTable ds = new DataTable();
                // Fill the DataSet.
                da.Fill(ds);
                DataTableaLista(ds);

                MailMessage mm = new MailMessage("mardismobile@gmail.com", ds.Rows[0][0].ToString()); //ds.Rows[0][0].ToString());
                mm.Subject = "Notificación [" + ds.Rows[0][1].ToString()+"]";
                mm.Body = "Tipo: " + ds.Rows[0][2].ToString() + " <br /><br />Usuario: " + ds.Rows[0][4].ToString() + "<br /><br />Local: " + ds.Rows[0][5].ToString() + "-" + ds.Rows[0][6].ToString() + "[" + ds.Rows[0][7].ToString() + "]<br />" + ds.Rows[0][3].ToString()+"<br />";
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "mardismobile@gmail.com";
                NetworkCred.Password = "Mardis$$.com2013";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
                return true;
                //lblMessage.Text = "Email Sent SucessFully.";
            }
            catch (Exception ex)
            { return false; }
            


        }


        public List<Dictionary<string, object>> CrearSuperNotificacion(string usu)
        {
          
                // Create the command and the connection.
                string sql = "[sp_extrae_notificacion_super] '" + usu +"'";
                SqlConnection con = new SqlConnection(rm.GetString("connectionString"));
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataTable ds = new DataTable();
                // Fill the DataSet.
                da.Fill(ds);
                return DataTableaLista(ds);
         
        }



        /*****************************TOOL********************/
        public List<Dictionary<string, object>> DataTableaLista(DataTable dt)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new

            System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows =
              new List<Dictionary<string, object>>();
            Dictionary<string, object> row = null;

            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName.Trim(), dr[col]);
                }
                rows.Add(row);
            }
            return rows;
        }
    }
}